﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using VFramework;
using DG.Tweening;

namespace VFramework{


	public enum VUIPopWindowStyle {
		NONE = 0,
		SLIDE,
		SCALE,
	}

	public enum VUISlideWindowDir {
		UP = 0,
		DOWN,
		LEFT,
		RIGHT
	}

	/**
	 * 弹出窗口类,使用的时候继承改对象
	 * 根节点为一个panel
	 * panel
	 *    |
	 * 	  root节点（窗口节点）
	 * 		  |
	 * 		btn_close btn_sure (窗口节点下面的两个按钮)
	 * 
	 * 若需要改变 show 重写 show方法
	 * hide
	 * sure方法
	**/
	public class VUIPopWindow : MonoBehaviour {


		protected GameObject root;
		protected GameObject windowRoot;
		protected GameObject closeBtn;
		protected GameObject sureBtn;
		protected Canvas canvas;
		protected bool isMove = false;
		protected bool isHide = true;
		protected Vector3 originPosition;

		public float showTime = 0.6f;
		public float offsetOutScreen = 30;
		public VUIPopWindowStyle popWindowStyle = VUIPopWindowStyle.SLIDE;
		public VUISlideWindowDir slideDir = VUISlideWindowDir.UP;

		protected bool _isStarted = false;
		private GameObject getObjFromParent(GameObject obj, string name){
			Transform t = obj.transform.FindChild (name);
			if (t != null) 
				return t.gameObject;
			return null;
		}

		protected virtual void _start(){
			if (_isStarted == false) {
				VLogger.debug (string.Format ("PopWindow {0} invoke start();", this.GetType ().Name));
				root = gameObject;
				canvas = Util.getCanvas (root);

				if (canvas == null) {
					VLogger.error ("cannot get canvas , is gameobj under canvas ?");
					throw new UnityException ("cannot get canvas , is gameobj under canvas ?");
				}

				windowRoot = root.transform.FindChild ("root").gameObject;
				originPosition = windowRoot.transform.localPosition;

				closeBtn = getObjFromParent (windowRoot, "btn_close");
				sureBtn = getObjFromParent (windowRoot, "btn_sure");
				initEvent ();
				_isStarted = true;
			}
		}


		void initEvent(){
			if (closeBtn != null) {
				
				closeBtn.GetComponent<Button>().onClick.AddListener (delegate() {
					hide();
				});
			}

			if (sureBtn != null) {
				sureBtn.GetComponent<Button>().onClick.AddListener (delegate() {
					sure();
				});
			}
		}

		protected void _update(){
		
		}

		private Vector3 getFromPositionByDIR(VUISlideWindowDir dir , Vector3 origin){
			Vector3 f = new Vector3 (origin.x,origin.y,origin.z);
			Vector2 canvasSize = canvas.GetComponent<RectTransform> ().sizeDelta;
			Vector2 popWindowSize = windowRoot.GetComponent<RectTransform> ().sizeDelta;


			if (dir == VUISlideWindowDir.DOWN) {
				f.y = canvasSize.y * 0.5f + popWindowSize.y * 0.5f + offsetOutScreen;
			} else if (dir == VUISlideWindowDir.LEFT) {
				f.x = -canvasSize.x * 0.5f - popWindowSize.x * 0.5f - offsetOutScreen;
			} else if (dir == VUISlideWindowDir.RIGHT) {
				f.x = canvasSize.x * 0.5f + popWindowSize.x * 0.5f + offsetOutScreen;
			} else if (dir == VUISlideWindowDir.UP) {
				f.y = -canvasSize.y * 0.5f - popWindowSize.y * 0.5f - offsetOutScreen;
			}

			return f;
		}

		/// <summary>
		/// 窗口出现
		/// </summary>
		public virtual void show(){
			if (_isStarted == false) {
				_start ();
			}

			if (isMove || !isHide) {
				return;
			}

			VLogger.debug (string.Format("PopWindow {0} invoke show();",this.GetType().Name));

			root.SetActive (true);
			Util.setAlpha (root.GetComponent<Image> ());

			float time = popWindowStyle == VUIPopWindowStyle.NONE ? 0 : showTime;
			isMove = true;
			root.GetComponent<Image> ().DOFade (0.5f,time);


			windowRoot.transform.localPosition = getFromPositionByDIR (slideDir , originPosition);
			windowRoot.transform.DOLocalMove (originPosition, time).SetEase (Ease.OutExpo).OnComplete (delegate {
				isMove = false;
				isHide = false;
			});
		}

		/// <summary>
		/// 窗口隐藏
		/// </summary>
		public virtual void hide(){

			VLogger.debug ("hide");
			VLogger.debug (isMove + "");
			VLogger.debug (isHide + "");
			if (isMove || isHide) {
				return;
			}

			isMove = true;

			float time = popWindowStyle == VUIPopWindowStyle.NONE ? 0 : showTime;
		
			root.GetComponent<Image> ().DOFade (0,time);
			windowRoot.transform.DOLocalMove (getFromPositionByDIR(slideDir,originPosition), time).OnComplete (delegate {
				isMove = false;
				isHide = true;
				root.SetActive(false);
			});



			VLogger.debug (string.Format("PopWindow {0} invoke hide();",this.GetType().Name));
		}

		/// <summary>
		/// 窗口的确认按钮点击事件
		/// </summary>
		public virtual void sure(){
			VLogger.debug (string.Format("PopWindow {0} invoke sure();",this.GetType().Name));
		}
	}
}